﻿unit LoadFiles;



interface
uses
  SysUtils,Classes,FileInfo,Windows,dialogs;

procedure LoadFile(var FileList : TList;FilePath:string);

//包含子目录
procedure LoadFileChild(var FileList : TList;FilePath:string; IsChecked : boolean);

procedure LoadCSV(var FileList : TList;FilePath:string);

implementation

procedure LoadFile(var FileList : TList;FilePath:string);
var
  SR : TSearchRec;
  MyFileInfo : PFileInfo;
begin
  try
    if FindFirst(FilePath + '\*',faAnyFile,SR)=0 then
    begin
      repeat
        new(MyFileInfo);
        MyFileInfo.FileName := SR.Name;
        MyFileInfo.FilePath := FilePath;
        MyFileInfo.FileType := ExtractFileExt(SR.Name);
        if (SR.Attr and faDirectory) = faDirectory then
        begin
           MyFileInfo.FileType := 'file';
        end;
        MyFileInfo.FileSize := SR.Size div 1024;
        MyFileInfo.FileDate := DateTimeToStr(FileDateToDateTime(SR.Time));
        FileList.Add(MyFileInfo);
      until FindNext(SR)<>0;
      SysUtils.FindClose(SR);
    end;
  finally

  end;
end;

//包含子目录
procedure LoadFileChild(var FileList : TList;FilePath:string; IsChecked : boolean);
var
  SR : TSearchRec;
  MyFileInfo : PFileInfo;
  LocalFileTime: TFileTime;
  SystemTime: TSystemTime;
  DateTime: TDateTime;
  I,Index : Integer;
begin
  for I := 0 to Length(FilePath) - 1 do
  begin
    if (FilePath[I]<>'\') then
    begin
      Index := I;
    end;
  end;
  FilePath := Copy(FilePath,0,Index+1);
  showmessage(FilePath);
  try
    if FindFirst(FilePath + '\*',faAnyFile,SR)=0 then
    begin
      repeat
        if(SR.Name <> '.') and (SR.Name <> '..')  then
        begin
        new(MyFileInfo);
        MyFileInfo.FileName := SR.Name;
        MyFileInfo.FilePath := FilePath;
        MyFileInfo.FileType := ExtractFileExt(SR.Name);
        if (SR.Attr and faDirectory) = faDirectory then
        begin
           MyFileInfo.FileType := 'file';
        end;
        MyFileInfo.FileSize := SR.Size div 1024;
        //MyFileInfo.FileDate := DateTimeToStr(FileDateToDateTime(SR.Time));
        FileTimeToLocalFileTime(SR.FindData.ftCreationTime, LocalFileTime);
        FileTimeToSystemTime(LocalFileTime, SystemTime);
        DateTime := SystemTimeToDateTime(SystemTime);
        MyFileInfo.FileDate := DateTimeToStr(DateTime);
        FileList.Add(MyFileInfo);
        if ((SR.Attr and faDirectory) = faDirectory) and IsChecked then
        begin
          if(SR.Name <> '.') and (SR.Name <> '..')  then
          begin
            LoadFileChild(FileList,FilePath+'\'+SR.Name,IsChecked);
          end;
        end;
      end;


      until FindNext(SR)<>0;
      SysUtils.FindClose(SR);
    end;
  finally

  end;
end;

procedure LoadCSV(var FileList : TList;FilePath:string);
var
  SL,SLD : TStringList;
  I,J : Integer;
  MyFileInfo : PFileInfo;
begin
  FileList.Clear;
  SL := TStringList.Create;
  SLD := TStringList.Create;
  SLD.StrictDelimiter := true;  //严格按照指定的分隔符，默认遇到空格会换行
  SL.LoadFromFile(FilePath);
  for I := 0 to SL.Count-1 do
  begin
    SLD.DelimitedText := SL[I];
    if (SLD.Count<5) then
    begin
      showmessage('The content is not in the corret format.');
      exit;
    end;
    new(MyFileInfo);
    MyFileInfo.FileName := SLD[0];
    MyFileInfo.FilePath := SLD[1];
    MyFileInfo.FileType := SLD[2];
    MyFileInfo.FileSize := StrToInt(Copy(SLD[3],0,Length(SLD[3])-2));
    MyFileInfo.FileDate := SLD[4];
    FileList.Add(MyFileInfo);
  end;
  if (FileList.Count = 0) then
  begin
    showmessage('The content is empty.');
    exit;
  end;
  SL.Free;
  SLD.Free;
end;

end.
